home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / msysjour / vol06 / 01 / kermit / wnkerm.c < prev    next >
C/C++ Source or Header  |  1990-12-31  |  13KB  |  388 lines

  1.  
  2. /* WARNING -- This C source program generated by Wart preprocessor. */
  3. /* Do not edit this file; edit the Wart-format source file instead, */
  4. /* and then run it through Wart to produce a new C source file.     */
  5.  
  6. /* Wart Version Info: */
  7. char *wartv = "Wart Version 1A(006) Jan 1989";
  8.  
  9. /*
  10.  * WNKERM.W
  11.  * Protocol module for Windows Kermit
  12.  * 
  13.  * Copyright (c) 1990 by
  14.  * William S. Hall
  15.  * 3665 Benton Street  #66
  16.  * Santa Clara, CA 95051
  17.  *
  18.  * This module must be preprocessed by wart
  19.  */
  20.  
  21. #define NOCOMM
  22. #define NOKANJI
  23. #define NOSOUND
  24. #define NOATOM
  25. #include <windows.h>
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include "wnkerm.h"
  29.  
  30. /* The state machine returns 0 to show completion or 1 if more input
  31.  * is expected.  Currently, these return values are not used.
  32.  */
  33. #define RESUME return(0)
  34. #define CONTINUE return(1)
  35.  
  36. /* local function prototypes */
  37. static int near input(void);
  38.  
  39. /* protocol states */
  40. #define rsinit 1
  41. #define rsfile 2
  42. #define rsdata 3
  43. #define ssinit 4
  44. #define ssfile 5
  45. #define ssdata 6
  46. #define sseof 7
  47. #define sseot 8
  48.  
  49. /* Protocol description.
  50.  */
  51.  
  52. #define BEGIN state =
  53.  
  54. int state = 0;
  55.  
  56. wart()
  57. {
  58.     int c,actno;
  59.     extern short tbl[];
  60.     while (1) {
  61.     c = input();
  62.     if ((actno = tbl[c + state*128]) != -1)
  63.         switch(actno) {
  64. case 1:
  65.     {        
  66.     krm_tinit();            /* initialize */
  67.     Kermit.start = 'w';            /* switch to wait */
  68. }
  69.     break;
  70. case 2:
  71.     {
  72.     if (Kermit.delay) {            /* timer will reset this parameter */
  73.     Kermit.start = 'w';        /* wait to send first packet */
  74.         CONTINUE;
  75.     } else {
  76.         if (krm_sinit() < 0) {            /* transmit sendinit packet */
  77.             krm_err(IDS_KRM_SENDINIT_ERROR);    /* error; inform remote */
  78.             RESUME;                /* quit */
  79.         }    
  80.     krmFlushQue();        /* flush any pending input in local buffer */
  81.         BEGIN ssinit;        /* prepare to open file and send file name */
  82.     }    
  83. }
  84.     break;
  85. case 3:
  86.     {
  87.     krm_tinit();            /* initialize */
  88.     BEGIN rsinit;            /* prepare to receive init packet */
  89. }
  90.     break;
  91. case 4:
  92.     {
  93.     krm_spar(krm_rcvpkt.data, krm_rcvpkt.len);    /* set init data */
  94.     Kermit.bctu = Kermit.bctr;            /* select block check type */
  95.     Kermit.pFile = krm_getnextfile(TRUE);    /* get file to send */
  96.     if (krm_sfile() < 0) {            /* send file name packet */
  97.         krm_err(IDS_KRM_SENDFILENAME_ERROR);    /* error; inform remote */
  98.         RESUME;                    /* quit */
  99.     }
  100.     BEGIN ssfile;               /* prepare to send first data packet */
  101. }
  102.     break;
  103. case 5:
  104.     {
  105.     int x;
  106.     krm_savename();            /* post the remote's name for file */
  107.     if ((x = krm_sdata()) == 0) {    /* no data in file */
  108.         if (krm_seof("") < 0) {        /* send end of file */
  109.            krm_err(IDS_KRM_SENDEOF_ERROR);   /* error; inform remote */
  110.            RESUME;            /* quit */
  111.         }
  112.         BEGIN sseof;
  113.     }
  114.     else if (x < 0) {            /* error in sending file */
  115.         krm_rclose(FALSE);        /* close the file */
  116.         krm_err(IDS_KRM_SENDDATA_ERROR); /* post error to remote and quit */
  117.         RESUME;
  118.     }
  119.     else
  120.     BEGIN ssdata;            /* prepare to receive data */
  121. }
  122.     break;
  123. case 6:
  124.     {                
  125.     krm_checkcnx();            /* check if transaction cancelled */
  126.     if (Kermit.abort) {
  127.     if (Kermit.abort == KRM_FILEABORT)  /* cancel file send */
  128.         Kermit.abort = 0;
  129.     if (krm_seof("D") < 0) {        /* tell remote about cancellation */
  130.        krm_err(IDS_KRM_SENDEOF_ERROR);  /* error; inform remote */
  131.        RESUME;                /* quit */
  132.     }
  133.         BEGIN sseof;            /* prepare to send end of file */
  134.     }
  135.     else {
  136.         int x;
  137.     if ((x = krm_sdata()) == 0) {        /* no more data */
  138.         if (krm_seof("") < 0) {        /* send end of file */
  139.            krm_err(IDS_KRM_SENDEOF_ERROR);  /* error; inform remote */
  140.            RESUME;                /* quit */
  141.         }
  142.             BEGIN sseof;        /* prepare to send end of file */
  143.         }
  144.     else if (x < 0) {            /* error */
  145.         krm_rclose(FALSE);            /* close the file */
  146.         krm_err(IDS_KRM_SENDDATA_ERROR);    /* inform remote */
  147.         RESUME;                /* quit */
  148.         }
  149.     }
  150. }
  151.     break;
  152. case 7:
  153.     {
  154.     if (Kermit.pFile = krm_getnextfile(FALSE)) {  /* any more files? */
  155.         if (krm_sfile() < 0) {              /* send next file name */
  156.             krm_err(IDS_KRM_SENDFILENAME_ERROR);  /* error, inform remote */
  157.             RESUME;                  /* quit */
  158.         }
  159.         BEGIN ssfile;                /* prepare for next file */
  160.     }    
  161.     else {                    /* no more files to send */
  162.     if (krm_seot() < 0) {            /* send break */
  163.         krm_err(IDS_KRM_SENDEOT_ERROR);    /* error, inform remote */
  164.         RESUME;                /* quit */
  165.     }
  166.     BEGIN sseot;                /* prepare to exit file send */
  167.     }
  168. }
  169.     break;
  170. case 8:
  171.     {
  172.     krm_tend(IDS_KRM_TRANSACTION_DONE);        /* clean up */
  173.     RESUME;                    /* quit */
  174. }
  175.     break;
  176. case 9:
  177.     {
  178.     BYTE data[KRM_MAXDATALEN + 1];
  179.     krm_spar(krm_rcvpkt.data, krm_rcvpkt.len);    /* read send-init data */
  180.     krm_ack(krm_rpar(data), data);        /* return init data in ACK */
  181.     Kermit.bctu = Kermit.bctr;            /* set block check type */
  182.     BEGIN rsfile;                    /* prepare for file name */
  183. }
  184.     break;
  185. case 10:
  186.     {
  187.     if (krm_rcvfil()) {                /* try to open file */
  188.     char buf[KRM_MAXDATALEN + 1];            
  189.     int inlen, outlen;
  190.     inlen = strlen(Kermit.pFile);        /* return our name to remote */
  191.     outlen = krm_encode(buf, Kermit.pFile, sizeof(buf) - 1, &inlen);
  192.         krm_ack(outlen, buf);            /* send the name in ACK */
  193.         BEGIN rsdata;                /* prepare to receive data */
  194.     }
  195.     else {
  196.     krm_err(IDS_KRM_FILE_OPEN_ERROR);    /* error, inform remote */
  197.     RESUME;                    /* quit */
  198.     }
  199. }
  200.     break;
  201. case 11:
  202.     {
  203.     if (krm_rcvdata()) {
  204.     switch(Kermit.abort) {            /* see if abort flag is on */
  205.         case KRM_BATCHABORT:
  206.         krm_ack(1, "Z");        /* cancel remaining files */
  207.         Kermit.abort = 0;
  208.         break;
  209.         case KRM_FILEABORT:
  210.         krm_ack(1, "X");        /* cancel current file */
  211.         Kermit.abort = 0;
  212.         break;
  213.         default:
  214.         krm_ack(0, "");                /* continue */
  215.     }
  216.     }
  217.     else {
  218.     krm_rclose(TRUE);            /* error in storing data */
  219.     krm_err(IDS_KRM_FILE_WRITE_ERROR);    /* inform host */
  220.     RESUME;                    /* quit */
  221.     }
  222. }
  223.     break;
  224. case 12:
  225.     {        /* see if remote wants us to delete file */
  226.     if (krm_rclose(krm_rcvpkt.len && 
  227.     (krm_rcvpkt.data[0] == 'D') ? TRUE : FALSE)) {
  228.         krm_ack(0, "");                /* acknowledge */
  229.         BEGIN rsfile;                /* prepare for next file */
  230.     }
  231.     else {
  232.     krm_err(IDS_KRM_FILE_CLOSE_ERROR);    /* error; inform remote */
  233.     RESUME;                    /* quit */
  234.     }
  235. }
  236.     break;
  237. case 13:
  238.     {
  239.     krm_ack(0, "");            /* acknowledge */
  240.     krm_tend(IDS_KRM_TRANSACTION_DONE);    /* clean up */
  241.     RESUME;                /* quit */
  242. }
  243.     break;
  244. case 14:
  245.     {
  246.     CONTINUE;            /* continue until packet is filled */
  247. }
  248.     break;
  249. case 15:
  250.     {
  251.     krm_rclose(TRUE);        /* close any open files */
  252.     krm_tend(KRM_ERROR_PACKET);    /* clean up */
  253.     RESUME;            /* quit */
  254. }
  255.     break;
  256. case 16:
  257.     {
  258.     krm_rclose(TRUE);            /* close any open files */
  259.     krm_err(IDS_KRM_UNKNOWN_PACKET);    /* post error to remote */
  260.     RESUME;                /* quit */
  261. }
  262.     break;
  263.  
  264.         }
  265.     }
  266. }
  267.  
  268. short tbl[] = {
  269. -1, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 
  270. 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 
  271. 16, 16, 16, 16, 14, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 
  272. 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 
  273. 16, 16, 16, 16, 16, 15, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 
  274. 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 
  275. 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 
  276. 16, 16, 16,  1, 16, 16,  3,  2, 16, 16, 16, 16, 16, 16, 16, 16, 
  277. -1, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 
  278. 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 
  279. 16, 16, 16, 16, 14, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 
  280. 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 
  281. 16, 16, 16, 16, 16, 15, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 
  282. 16, 16, 16,  9, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 
  283. 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 
  284. 16, 16, 16,  1, 16, 16,  3,  2, 16, 16, 16, 16, 16, 16, 16, 16, 
  285. -1, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 
  286. 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 
  287. 16, 16, 16, 16, 14, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 
  288. 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 
  289. 16, 16, 13, 16, 16, 15, 10, 16, 16, 16, 16, 16, 16, 16, 16, 16, 
  290. 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 
  291. 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 
  292. 16, 16, 16,  1, 16, 16,  3,  2, 16, 16, 16, 16, 16, 16, 16, 16, 
  293. -1, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 
  294. 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 
  295. 16, 16, 16, 16, 14, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 
  296. 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 
  297. 16, 16, 16, 16, 11, 15, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 
  298. 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 12, 16, 16, 16, 16, 16, 
  299. 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 
  300. 16, 16, 16,  1, 16, 16,  3,  2, 16, 16, 16, 16, 16, 16, 16, 16, 
  301. -1, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 
  302. 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 
  303. 16, 16, 16, 16, 14, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 
  304. 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 
  305. 16, 16, 16, 16, 16, 15, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 
  306. 16, 16, 16, 16, 16, 16, 16, 16, 16,  4, 16, 16, 16, 16, 16, 16, 
  307. 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 
  308. 16, 16, 16,  1, 16, 16,  3,  2, 16, 16, 16, 16, 16, 16, 16, 16, 
  309. -1, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 
  310. 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 
  311. 16, 16, 16, 16, 14, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 
  312. 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 
  313. 16, 16, 16, 16, 16, 15, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 
  314. 16, 16, 16, 16, 16, 16, 16, 16, 16,  5, 16, 16, 16, 16, 16, 16, 
  315. 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 
  316. 16, 16, 16,  1, 16, 16,  3,  2, 16, 16, 16, 16, 16, 16, 16, 16, 
  317. -1, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 
  318. 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 
  319. 16, 16, 16, 16, 14, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 
  320. 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 
  321. 16, 16, 16, 16, 16, 15, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 
  322. 16, 16, 16, 16, 16, 16, 16, 16, 16,  6, 16, 16, 16, 16, 16, 16, 
  323. 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 
  324. 16, 16, 16,  1, 16, 16,  3,  2, 16, 16, 16, 16, 16, 16, 16, 16, 
  325. -1, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 
  326. 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 
  327. 16, 16, 16, 16, 14, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 
  328. 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 
  329. 16, 16, 16, 16, 16, 15, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 
  330. 16, 16, 16, 16, 16, 16, 16, 16, 16,  7, 16, 16, 16, 16, 16, 16, 
  331. 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 
  332. 16, 16, 16,  1, 16, 16,  3,  2, 16, 16, 16, 16, 16, 16, 16, 16, 
  333.  0, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 
  334. 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 
  335. 16, 16, 16, 16, 14, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 
  336. 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 
  337. 16, 16, 16, 16, 16, 15, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 
  338. 16, 16, 16, 16, 16, 16, 16, 16, 16,  8, 16, 16, 16, 16, 16, 16, 
  339. 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 
  340. 16, 16, 16,  1, 16, 16,  3,  2, 16, 16, 16, 16, 16, 16, 16, 16, 
  341. };
  342.  
  343.  
  344. /* input
  345.  *
  346.  * provide packet type to state machine
  347.  */
  348. static int near input()
  349. {
  350.  
  351.     register int type;
  352.  
  353.     if (Kermit.start) {
  354.     type = Kermit.start;
  355.     Kermit.start = 0;
  356.     }
  357.     else {
  358.     type = krm_rpack();        /* read input */
  359.     if ((type != '$') && (type != 'E'))  {
  360.       /* packet is complete but is not an error packet */
  361.         if ((Kermit.seq != krm_rcvpkt.seq) || strchr("NQT", type)) {
  362.           /* something is wrong with packet */
  363.             if (Kermit.retries >= KermParams.RetryLimit) {
  364.           /* pretend we have an error packet */
  365.             krmCreatePseudoPacket('E', PS_DONE, IDS_KRM_TOOMANYRETRIES);
  366.             type = 'E';
  367.             }
  368.             else if ((type == 'N') && 
  369.               (krm_rcvpkt.seq == ((Kermit.seq + 1) & 63))) {
  370.           /* this is OK since NAK for next packet is ACK for previous */
  371.             type = 'Y';
  372.             Kermit.retries = 0;
  373.             }
  374.             else {
  375.           /* resend previous packet and try again */
  376.             Kermit.retries += 1;
  377.             Kermit.totalretries += 1;
  378.             krm_resend();
  379.             type = '$';
  380.             }
  381.         }
  382.         else    /* packet is OK; reset retry count */
  383.             Kermit.retries = 0;
  384.     }
  385.     }
  386.     return (type);
  387. }
  388.